#include "tools.h"

#include <iostream>
#include <QDir>
#include <QDebug>

//编译器选项对话框实现
CompilerOptions::CompilerOptions(QWidget *parent)
    : QDialog(parent)
{
    std::cout <<"as" <<std::endl;
    this->resize(600, 200);
    this->setWindowTitle("编译设置");

    QVBoxLayout *layout = new QVBoxLayout(this);

    a = new QLabel(tr("添加编译选项:"), this);
    a->setFixedSize(400, 20);
    layout->addWidget(a);

    compilerOptionBox = new QLineEdit(this);
    compilerOptionBox->setText("");
    compilerOptions = "";
    layout->addWidget(compilerOptionBox);

    okButton = new QPushButton(tr("OK"), this);
    cancelButton = new QPushButton(tr("Cancel"), this);
    layout->addWidget(okButton);
    layout->addWidget(cancelButton);

    connect(okButton, &QPushButton::clicked, this, &CompilerOptions::apply);
    connect(cancelButton, &QPushButton::clicked, this, [=] {compilerOptionBox->setText(compilerOptions); reject();});

    QString currentPath = QDir::currentPath();
    settingsFilePath = currentPath + "/settings2.ini";

    // load
    QSettings settings2(settingsFilePath, QSettings::IniFormat);

    if (QFile::exists(settingsFilePath))
    {
        compilerOptions = settings2.value("compilerOptions", "").toString();
        compilerOptionBox->setText(compilerOptions);
    }
}

void CompilerOptions::apply() {
    compilerOptions = compilerOptionBox->text();
    std::cout << compilerOptions.toStdString() << std::endl;
    QSettings settings2(settingsFilePath, QSettings::IniFormat);

    settings2.setValue("compilerOptions", compilerOptions);

    settings2.sync();

    accept();
}

CompilerOptions::~CompilerOptions() {}

void CompilerOptions::openCompilerOptions()
{
    exec();
}

// 工具设置对话框实现
ToolsSettings::ToolsSettings(QWidget *parent)
    : QDialog(parent)
{

    this->resize(700, 500);
    this->setWindowTitle("编辑器选项");

    QVBoxLayout *mainLayout = new QVBoxLayout(this); // 创建主布局，使用垂直布局

    // 创建字体选择布局，使用水平布局
    QHBoxLayout *fontLayout = new QHBoxLayout();
    QLabel *fontLabel = new QLabel(tr("字体"), this);
    fontComboBox = new QFontComboBox(this);
    fontComboBox->setCurrentFont(QFont("Consolas"));
    fontComboBox->setFixedSize(400, 50);

    fontLayout->addWidget(fontLabel);
    fontLayout->addWidget(fontComboBox);

    // 创建字体大小选择布局，使用水平布局
    QHBoxLayout *sizeLayout = new QHBoxLayout();
    QLabel *sizeLabel = new QLabel(tr("字体大小"), this);
    fontSizeSpinBox = new QSpinBox(this);
    fontSizeSpinBox->setRange(8, 72);
    fontSizeSpinBox->setValue(25);
    fontSizeSpinBox->setFixedSize(400, 40);


    currentFont.setPixelSize(25);
    currentFont.setFamily("Consolas");

    sizeLayout->addWidget(sizeLabel);
    sizeLayout->addWidget(fontSizeSpinBox);

    // 主题
    QHBoxLayout *themeLayout = new QHBoxLayout();
    QLabel *themeLabel = new QLabel(tr("主题"), this);
    themeComboBox = new QComboBox(this);
    themeComboBox->addItem("Light");
    themeComboBox->addItem("Dark");
    themeComboBox->setFixedSize(400, 30);

    themeLayout->addWidget(themeLabel);
    themeLayout->addWidget(themeComboBox);

    // 将字体选择布局和大小选择布局添加到主布局
    mainLayout->addLayout(fontLayout);
    mainLayout->addLayout(sizeLayout);
    mainLayout->addLayout(themeLayout);

    // 创建选择背景图片按钮布局，使用水平布局
    QHBoxLayout *backgroundLayout = new QHBoxLayout();
    backgroundLabel = new QLabel(tr("背景图片:"), this);
    QPushButton *backgroundButton = new QPushButton(tr("选择图片"), this);
    QPushButton *clearPicButton = new QPushButton(tr("清空选择"), this);
    backgroundLayout->addWidget(backgroundLabel);
    backgroundLayout->addWidget(backgroundButton);
    backgroundLayout->addWidget(clearPicButton);

    // 将选择背景图片按钮布局添加到主布局
    mainLayout->addLayout(backgroundLayout);

    // 创建背景不透明度调节按钮，使用水平布局
    QHBoxLayout *opacityLayout = new QHBoxLayout();
    QLabel *opacityLabel1 = new QLabel(tr("背景不透明度"), this);
    opacitySlider = new QSlider(Qt::Horizontal, this);
    //QLabel *opacityLabel2 = new QLabel("100%", this);
    opacitySlider->setRange(0, 100); // 不透明度范围从 0 到 100
    opacitySlider->setValue(20); // 默认值为 50%
    opacity = 0.2;
    opacityLayout->addWidget(opacityLabel1);
    opacityLayout->addWidget(opacitySlider);
    //opacityLayout->addWidget(opacityLabel2);


    mainLayout->addLayout(opacityLayout);

     // 创建按钮布局，使用水平布局
    QHBoxLayout *buttonLayout = new QHBoxLayout();
    okButton = new QPushButton(tr("OK"), this);
    cancelButton = new QPushButton(tr("Cancel"), this);
    buttonLayout->addWidget(okButton);
    buttonLayout->addWidget(cancelButton);

    mainLayout->addLayout(buttonLayout);

    //保存设置文件路径
    QString currentPath = QDir::currentPath();
    settingsFilePath = currentPath + "/settings.ini";

    loadSettings(imagePath, opacity);

    connect(okButton, &QPushButton::clicked, this, &ToolsSettings::applySettings);
    connect(cancelButton, &QPushButton::clicked, this, &QDialog::reject);
    connect(backgroundButton, &QPushButton::clicked, this, &ToolsSettings::chooseBackgroundImage);
    connect(clearPicButton, &QPushButton::clicked, this, &ToolsSettings::clearPic);
    connect(opacitySlider, &QSlider::valueChanged, this, &ToolsSettings::updateOpacity);
    connect(this, &ToolsSettings::fontSettingsChanged, this, &ToolsSettings::applyFontSettings);
    connect(this, &ToolsSettings::rejected, this, &ToolsSettings::onReject);

}

ToolsSettings::~ToolsSettings() {
    saveSettings(settingsFilePath);
    //std::cout << "!" << settingsFilePath.toLocal8Bit().toStdString() << std::endl;
}


void ToolsSettings::applySettings()
{
    // 设置字体
    QFont font = fontComboBox->currentFont();
    // 设置字号
    font.setPixelSize(fontSizeSpinBox->value());

    currentTheme = themeComboBox->currentText();

    emit fontSettingsChanged(font);

    accept();

}

void ToolsSettings::saveSettings(const QString &filePath)
{
    QSettings settings(filePath, QSettings::IniFormat);

    settings.setValue("font", fontComboBox->currentFont().toString());
    settings.setValue("fontSize", fontSizeSpinBox->value());
    settings.setValue("theme", themeComboBox->currentText());
    settings.setValue("backgroundImage", imagePath);
    settings.setValue("opacity", (int)(opacity * 100));

    settings.sync();
}

void ToolsSettings::loadSettings(QString &imagePath, qreal &opacity)
{
    //std::cout << "loadSettings()" << std::endl;
    QSettings settings(QDir::currentPath() + "/settings.ini", QSettings::IniFormat);

    if (QFile::exists(QDir::currentPath() + "/settings.ini"))
    {
        //std::cout << "loadSettings()" << std::endl;
        currentFont.fromString(settings.value("font", QFont("Consolas").toString()).toString());
        fontComboBox->setCurrentFont(currentFont);

        int fontSize = settings.value("fontSize", 25).toInt();
        fontSizeSpinBox->setValue(fontSize);
        currentFont.setPixelSize(fontSize);

        QString theme = settings.value("theme", "Light").toString();
        themeComboBox->setCurrentText(theme);
        currentTheme = theme;

        imagePath = settings.value("backgroundImage", "").toString();
        QFileInfo imageInfo(imagePath);
        backgroundLabel->setText("背景图片:" + imageInfo.fileName());

        int opacityValue = settings.value("opacity", 50).toInt();
        opacity = opacityValue / 100.0;
        opacitySlider->setValue((int)(opacity * 100));
    }
    /*
    else
     {
        // 使用默认设置
        fontComboBox->setCurrentFont(QFont("Consolas"));
        fontSizeSpinBox->setValue(25);
        opacitySlider->setValue(100);
        opacity = 1.0;
    }*/
}


void ToolsSettings::chooseBackgroundImage()
{
    // 打开文件对话框，让用户选择图片文件
    imagePath = QFileDialog::getOpenFileName(this, tr("选择背景图片"), "", tr("Images (*.png *.xpm *.jpg)"));
    QFileInfo imageInfo(imagePath);

    backgroundLabel->setText("背景图片:" + imageInfo.fileName());
}

void ToolsSettings::clearPic() {
    imagePath = "";
    QFileInfo imageInfo(imagePath);
    backgroundLabel->setText("背景图片:" + imageInfo.fileName());
}

void ToolsSettings::updateOpacity(int value)
{
    opacity = value / 100.0;
    // 更新标签文本
    //opacityLabel2->setText(QString::number(value) + "%");

}




void ToolsSettings::openToolsSettings()
{
    imagePathCopy = imagePath;
    opacityCopy = opacity;
    exec();
}

void ToolsSettings::applyFontSettings(const QFont &font)
{
    //std::cout << "applyFontSettings" << std::endl;
    currentFont = font;
}

void ToolsSettings::onReject() {
    fontSizeSpinBox->setValue(currentFont.pixelSize());
    fontComboBox->setCurrentFont(QFont(currentFont.family()));
    themeComboBox->setCurrentText(currentTheme);
    QFileInfo imageInfo(imagePathCopy);
    imagePath = imagePathCopy;
    backgroundLabel->setText("背景图片:" + imageInfo.fileName());
    opacity = opacityCopy;
    opacitySlider->setValue((int)(opacityCopy * 100));
}
